function [models,spec_Weight,spat_Weight,PCANet_TrnTime,LinearSVM_TrnTime]=...
    Mugnet_Extraction_training(ImgFormat,TrnData_Spec,TrnLabels_Spec,TrnData_Spat,PCANet,train_num)

fprintf('\n ====== MugNet Training ======= \n')

TrnData_ImgCell_Spec = mat2imgcell(TrnData_Spec,1,200,ImgFormat); % convert columns in TrnData to cells
TrnData_ImgCell_Spat = mat2imgcell(TrnData_Spat,9,200,ImgFormat);
clear TrnData_Spec;
clear TrnData_Spat;

tic;
% spectral mugnet
spec_Weight=cell(3,1);
spec_blockindex=cell(3,1);
for i=1:3
    % BlkIdx serves the purpose of learning block-wise DR projection matrix; e.g., WPCA
    [ftrain_spec,spec_Weight{i},spec_blockindex{i}] = PCANet_train_Spec(TrnData_ImgCell_Spec,PCANet,1,i);
    if i==1
        f_spec=ftrain_spec;
    else
        f_spec=[f_spec;ftrain_spec];%all patchsize
    end
end
f_train_spec=f_spec(:,1:size(train_num,1));
clear f_spec
clear TrnData_ImgCell_Spec;

% spatial mugnet
spat_Weight=cell(3,1);
spat_blockindex=cell(3,1);
for i=1:3
    % BlkIdx serves the purpose of learning block-wise DR projection matrix; e.g., WPCA
    [ftrain_spat,spat_Weight{i},spat_blockindex{i}] = PCANet_train_Spat(TrnData_ImgCell_Spat,PCANet,1,i);
    if i==1
        f_spat=ftrain_spat;
    else
        f_spat=[f_spat;ftrain_spat];%all patchsize
    end
end
f_train_spat=f_spat(:,1:size(train_num,1));
clear f_spat
clear TrnData_ImgCell_Spat;
% training set
f_train=[f_train_spec;f_train_spat];%choose the train sample,not include the zero sample
PCANet_TrnTime = toc;

%% Cross validation(optional)
%bestC=Mugnet_CrossValidation(TrnLabels_Spec,f_train)

fprintf('\n ====== Training Linear SVM Classifier ======= \n')

tic;
%s:1---L2-regularized L2-loss support vectorclassification (dual),q:quiet situation
models = train(TrnLabels_Spec, f_train', '-s 1 -q'); % we use linear SVM classifier (C = 1), calling libsvm library
LinearSVM_TrnTime = toc;
clear f_train;
end